package com.datawin.taotaocheche.ui.widget.wheel;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import com.datawin.taotaocheche.R;
import com.datawin.taotaocheche.ui.widget.wheel.adapter.AbstractWheelTextAdapter;
import com.datawin.taotaocheche.ui.widget.wheel.view.OnWheelChangedListener;
import com.datawin.taotaocheche.ui.widget.wheel.view.OnWheelScrollListener;
import com.datawin.taotaocheche.ui.widget.wheel.view.WheelView;

/**
 * 更改服务类型
 * 
 * @author ywl
 * 
 */
public class ChangeAddressDialog extends Dialog implements
		View.OnClickListener {

	private WheelView wvProvince;
	private View lyChangeAddress;
	private TextView btnSure;
	private TextView btnCancel;
	private TextView title;

	private Context context;

	private List<String> arrProvinces = new ArrayList<String>();
	private AddressTextAdapter provinceAdapter;

	private String strProvince;
	private OnAddressCListener onAddressCListener;

	private int maxsize = 24;
	private int minsize = 14;
	private int position;
	private String titlecontext="";

	public ChangeAddressDialog(Context context, 
			List<String> goodsList,String titleContext) {
		super(context, R.style.ShareDialog);
		this.context = context;
		arrProvinces = goodsList;
		titlecontext=titleContext;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_myinfo_changeaddress);
		title=(TextView) findViewById(R.id.tv_share_title);
		wvProvince = (WheelView) findViewById(R.id.wv_address_province);
		lyChangeAddress = findViewById(R.id.ly_myinfo_changeaddress);
		btnSure = (TextView) findViewById(R.id.btn_myinfo_sure);
		btnCancel = (TextView) findViewById(R.id.btn_myinfo_cancel);

		lyChangeAddress.setOnClickListener(this);
		btnSure.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		
		title.setText(titlecontext);
//		for (int j = 0; j < arrProvinces.size(); j++) {
//			if (arrProvinces.get(j).equals(strProvince)) {
//				position = j;
//				break;
//			}
//
//		}

		provinceAdapter = new AddressTextAdapter(context, arrProvinces, position,
				maxsize, minsize);
		wvProvince.setVisibleItems(3);
		wvProvince.setViewAdapter(provinceAdapter);
		wvProvince.setCurrentItem(position);

		wvProvince.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				strProvince = arrProvinces.get(wheel.getCurrentItem());
				position=wheel.getCurrentItem();
				setTextviewSize(strProvince, provinceAdapter);

			}
		});

		wvProvince.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				String currentText = (String) provinceAdapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, provinceAdapter);
			}
		});

	}

	private class AddressTextAdapter extends AbstractWheelTextAdapter {
		List<String> list;

		protected AddressTextAdapter(Context context, List<String> list,
				int currentItem, int maxsize, int minsize) {
			super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem,
					maxsize, minsize);
			this.list = list;
			setItemTextResource(R.id.tempValue);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			return view;
		}

		@Override
		public int getItemsCount() {
			return list.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			return list.get(index)+ "";
			
		}
	}

	/**
	 * 设置字体大小
	 * 
	 * @param curriteItemText
	 * @param adapter
	 */
	public void setTextviewSize(String curriteItemText,
			AddressTextAdapter adapter) {
		ArrayList<View> arrayList = adapter.getTestViews();
		int size = arrayList.size();
		String currentText;
		for (int i = 0; i < size; i++) {
			TextView textvew = (TextView) arrayList.get(i);
			currentText = textvew.getText().toString();
			if (curriteItemText.equals(currentText)) {
				textvew.setTextSize(24);
			} else {
				textvew.setTextSize(14);
			}
		}
	}

	public void setAddresskListener(OnAddressCListener onAddressCListener) {
		this.onAddressCListener = onAddressCListener;
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_myinfo_sure:
			if (onAddressCListener != null) {
				onAddressCListener.confirm(position, strProvince);
			}
			dismiss();
			break;

		case R.id.btn_myinfo_cancel:
			if (onAddressCListener != null) {
				onAddressCListener.cancel();
			}
			dismiss();
			break;

		default:
			break;
		}

	}

	/**
	 * 回调接口
	 * 
	 * @author Administrator
	 * 
	 */
	public interface OnAddressCListener {

		public void confirm(int position, String strProvince);

		public void cancel();
	}

	/**
	 * 初始化地点
	 * 
	 * @param province
	 * @param
	 */
	public void setAddress(String province) {
		if (province != null) {
			this.strProvince = province;
		}

	}

}